/***************************************************************************
 *   Copyright (C) 2010 by programmeboy                                    *
 *   programmeboy@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
 ***************************************************************************/

#include <ntddk.h>
#include "idt.h"

ULONG
QueryIDTEntryCounts(
	VOID
)
/*++

Routine Description:

    

Arguments:

	

Return Value:

--*/
{
	return (ULONG)MAX_IDT_ENTRIES;
}

NTSTATUS
QueryIDTEntrys(
	OUT PVOID Buffer,
	IN ULONG InputLength,
	OUT PULONG OutputLength
)
/*++

Routine Description:

    

Arguments:

	

Return Value:

--*/
{
	ULONG Counts;
	ULONG NeedBufferSize;
	IDTINFO IdtInfo;
	ULONG Index;
	PIDTENTRY IdtEntrys;
	PIDT_OUT_ENTRY OutEntrys;
	
	if (!Buffer || !InputLength){
		return STATUS_INVALID_PARAMETER;
	}

	//
	// First get the counts of IDT Entry.
	//
	
	Counts = QueryIDTEntryCounts();
	NeedBufferSize = Counts * sizeof(IDT_OUT_ENTRY);
	
	if (NeedBufferSize > InputLength){
		return STATUS_INSUFFICIENT_RESOURCES;
	}

	//
	// Get IDT's Routine Array Base.
	//
	
	__asm SIDT IdtInfo;
	IdtEntrys = (PIDTENTRY)MAKELONG(IdtInfo.LowIDTbase, IdtInfo.HiIDTbase);

	//
	// Go through the idt array.
	//
	
	OutEntrys = (PIDT_OUT_ENTRY)Buffer;
	for (Index = 0; Index < Counts; Index++){
		RtlZeroMemory(&OutEntrys[Index], sizeof(IDT_OUT_ENTRY));
		OutEntrys[Index].Routine = MAKELONG(IdtEntrys[Index].LowOffset, IdtEntrys[Index].HiOffset);
		OutEntrys[Index].Selector = IdtEntrys[Index].Selector;
		KdPrint(("Index:%d\tSelector:%d\tRoutine:0x%08x", Index, IdtEntrys[Index].Selector, 
                OutEntrys[Index].Routine));
	}
	
	//
	// Set the output length
	//
	
	if (OutputLength){
		*OutputLength = NeedBufferSize;
	}
	return STATUS_SUCCESS;
}